#include <faker/basic/pch.h>
#include <faker/engine/App.h>

using namespace Faker;

bool Engine::App::Create(const GLFWConfig &config)
{
    if (!BaseApp::Create(config))
    {
        return false;
    }

    if (!m_EngineSystem.InitResources())
    {
        return false;
    }

    if (!m_EngineGenerator.Generate(m_Scene))
    {
        return false;
    }

    m_EngineLayout.SetSceneRef(&m_Scene);

    FK_LOGI("Create Launcher app (edit mode )");
   
    Gut::RenderTarget::Desc desc;
    desc.width = this->state.mClientWidth;
    desc.height = this->state.mClientHeight;
    desc.numTexture = 1;

    this->m_RenderTarget.Create(desc);

    return true;
}

void Faker::Engine::App::OnFrameRender()
{
    glClearColor(1.0f, 0.5f, 0.5f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    m_RenderTarget.Begin();
    m_EngineSystem.RenderScene(m_Scene);
    m_RenderTarget.End();
}

void Faker::Engine::App::OnEdit()
{
    m_EngineLayout.Instance();
    m_EngineLayout.HandleSignal();

    m_EngineLayout.SetSceneTexture(m_RenderTarget.outColors[0]);
    
    // ImGui::Image((ImTextureID)m_RenderTarget.outColors[0].ID, ImVec2(400, 300), ImVec2(0, 1), ImVec2(1, 0));
};

void Faker::Engine::App::OnFrameBufferSize(int width,int height) 
{
    BaseApp::OnFrameBufferSize(width, height);
    // camera.SetAspect(GetAspect());
    m_EngineSystem.OnFrameBufferSize(width, height);

    m_RenderTarget.SetWindowSize(width, height);
}
